package util.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 数据库连接工具类
 * 
 * May 9, 2009
 *
 * @author Fabian
 *
 */
public class DBConn {
	//数据库信息
	private static String DRIVER_CLASS = "com.mysql.jdbc.Driver";
	private static String DB_NAME = "aust_lms";
	private static String USER_NAME = "root";
	private static String PASSWORD = "admin";
	//private static String CHARSET = "GBK";
	private static String URL = "jdbc:mysql://localhost:3306/" + DB_NAME;
		//+ "&useUnicode = true & characterEncoding = " + CHARSET;
	
	//Driver只注册一次
	static {
		try{
			Class.forName(DRIVER_CLASS);
			
		} catch (ClassNotFoundException e) {
			System.out.println("加载驱动器失败："+e.getMessage());
			e.printStackTrace();
		} 
	}
	
	/**
	 * 获取Connection
	 * @return
	 */
	public static Connection getConn() {
		Connection conn = null;
		try {
			conn = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
		} catch (SQLException e) {
			System.out.println("数据库建立连接失败："+e.getMessage());
			e.printStackTrace();
		}
		return conn;
	}
	
	/**
	 * 获取PreparedStatement
	 * @param conn
	 * @param sql
	 * @return
	 */
	public static PreparedStatement getPstmt(Connection conn, String sql) {
		PreparedStatement pstmt = null;
		try {
			pstmt = conn.prepareStatement(sql);
		} catch (SQLException e) {
			System.out.println("获取PreparedStatement失败："+e.getMessage());
			e.printStackTrace();
		}
		return pstmt;
	}
	
	/**
	 * 获取Statement
	 * @param conn
	 * @return
	 */
	public static Statement getStmt(Connection conn) {
		Statement stmt = null;
		try {
			stmt = conn.createStatement();
		} catch (SQLException e) {
			System.out.println("获取Statement失败："+e.getMessage());
			e.printStackTrace();
		}
		return stmt;
	}
	
	/**
	 * 执行sql语句
	 * @param stmt
	 * @param sql
	 * @return
	 */
	public static ResultSet executeQuery(Statement stmt, String sql) {
		ResultSet rs = null;
		try {
			rs = stmt.executeQuery(sql);
		} catch (SQLException e) {
			System.out.println("执行sql语句失败："+e.getMessage());
			e.printStackTrace();
		}
		return rs;
	}
	
	/**
	 * 关闭Connection
	 * @param conn
	 */
	public static void close(Connection conn) {
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				System.out.println("关闭Connection失败："+e.getMessage());
				e.printStackTrace();
			}
		} 
	}
	
	/**
	 * 关闭Statement
	 * @param stmt
	 */
	public static void close(Statement stmt) {
		if (stmt != null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				System.out.println("关闭Statement失败："+e.getMessage());
				e.printStackTrace();
			}
		}	
	}
	
	/**
	 * 关闭ResultSet
	 * @param rs
	 */
	public static void close(ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				System.out.println("关闭ResultSet失败："+e.getMessage());
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 测试
	 * @param args
	 */
	/*public static void main(String[] args) {
		Connection conn = DBConn.getConn();
		String sql = "select * from t_grade";
		PreparedStatement pstmt = DBConn.getPstmt(conn, sql);
		ResultSet rs = null;
		try {
			rs = pstmt.executeQuery();
			while (rs.next()) {
				System.out.println(rs.getInt(1));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBConn.close(conn);
			DBConn.close(pstmt);
			DBConn.close(rs);
		}
	}*/
}
